/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MailComposePanel.java
 *
 * Created on Jun 14, 2009, 10:21:47 PM
 */

package ui;

import bus.AccountBUS;
import bus.MessageBUS;
import dto.*;
import excom.JBoxEvent;
import java.util.ArrayList;
import outlook.Mailer;
import utils.Misc;
import utils.MsgboxHelper;

/**
 *
 * @author Hung Cuong <nhc.hcmuns at gmail.com>
 */
public class MailComposePanel extends AbstractFrame {

    //AccountDTO acc = new AccountDTO("java.outlook@gmail.com", "java.outlook123456", "smtp.gmail.com");
    //XMessage msg = new XMessage("jake@microsoft.com", "nhc.usa@gmail.com", "Test email", "This is a spam email");

    /** Creates new form MailComposePanel */
    private AccountDTO ca = null ;
   
    public MailComposePanel() {
        initComponents();

        getRootPane().setDefaultButton(btnSend);  // set default button
        setLocationRelativeTo(this);            // center the form

        
        txtTo.setText("to@gmail.com");
        txtSubject.setText("Subject");
        rtbBody.setText("Body");

        ArrayList<AccountDTO> arrCount = new AccountBUS().getAllRecords();
        if(arrCount.size() > 0)
        {
            ca = arrCount.get(0);
        }
        cbFrom.addItem(ca.getUsername());
        msgId = -1;
    }
    private int msgId;
    public void setMsgId(int id)
    {
        msgId = id;
        MessageDTO dto = new MessageBUS().getInfo(msgId);
        txtSubject.setText(dto.getSubject());
        txtTo.setText(dto.getTo());
        rtbBody.setText(dto.getBody());
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cbFrom = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtTo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtSubject = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        rtbBody = new javax.swing.JTextArea();
        btnSend = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        savedraft = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Compose email");
        setResizable(false);

        jLabel1.setText("From:");
        jLabel1.setName("jLabel1"); // NOI18N

        cbFrom.setName("cbFrom"); // NOI18N

        jLabel2.setText("To:");
        jLabel2.setName("jLabel2"); // NOI18N

        txtTo.setName("txtTo"); // NOI18N

        jLabel3.setText("Subject:");
        jLabel3.setName("jLabel3"); // NOI18N

        txtSubject.setName("txtSubject"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        rtbBody.setColumns(20);
        rtbBody.setRows(5);
        rtbBody.setName("rtbBody"); // NOI18N
        jScrollPane1.setViewportView(rtbBody);

        btnSend.setText("Send");
        btnSend.setName("btnSend"); // NOI18N
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        savedraft.setText("Draft");
        savedraft.setName("savedraft"); // NOI18N
        savedraft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savedraftActionPerformed(evt);
            }
        });

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText("File");
        jMenu1.setName("jMenu1"); // NOI18N
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbFrom, javax.swing.GroupLayout.Alignment.TRAILING, 0, 430, Short.MAX_VALUE)
                            .addComponent(txtTo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                            .addComponent(txtSubject, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 289, Short.MAX_VALUE)
                        .addComponent(savedraft)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSend)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSend)
                    .addComponent(btnCancel)
                    .addComponent(savedraft))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        //init message
       if(txtTo.getText().isEmpty() || txtSubject.getText().isEmpty()) {
            MsgboxHelper.showError("To Address and Subject cannot be empty!");
            return;
        }
        MessageDTO newMessage = CreateMessage("Outbox");
        if(msgId != -1)
        {   newMessage = new MessageBUS().getInfo(msgId);
            new MessageBUS().changeType(msgId, "Outbox");
        }
        Mailer.sendMail(ca,newMessage);        
        this.dispose();
        addJBoxEvent(new JBoxEvent(evt, "sendmessage"));
       
    }//GEN-LAST:event_btnSendActionPerformed

    private MessageDTO CreateMessage(String type){
        MessageDTO newMessage = new MessageDTO();

        newMessage.setFrom(ca.getUsername());
        newMessage.setTo(txtTo.getText());
        newMessage.setSubject(txtSubject.getText());
        newMessage.setBody(rtbBody.getText());
        newMessage.setType(type);
        newMessage.setSentDate(Misc.getDatetimeToString());
        
        new MessageBUS().insertMessage(newMessage);

        return newMessage;
    }

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void savedraftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savedraftActionPerformed
               
        if(msgId != -1)
            new MessageBUS().deleteMessage(new MessageBUS().getInfo(msgId));
        
        CreateMessage("Draft");
        this.dispose();
        addJBoxEvent(new JBoxEvent(evt, "savemessage"));
}//GEN-LAST:event_savedraftActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSend;
    private javax.swing.JComboBox cbFrom;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea rtbBody;
    private javax.swing.JButton savedraft;
    private javax.swing.JTextField txtSubject;
    private javax.swing.JTextField txtTo;
    // End of variables declaration//GEN-END:variables

}
